home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / manageme / tcpdump-.001 / tcpdump-~ / tcpdump-3.0.2-linux / libpcap-0.0.6 / etherent.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-21  |  3.2 KB  |  149 lines

  1. /*
  2.  * Copyright (c) 1990, 1993, 1994
  3.  *    The Regents of the University of California.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that: (1) source code distributions
  7.  * retain the above copyright notice and this paragraph in its entirety, (2)
  8.  * distributions including binary code include the above copyright notice and
  9.  * this paragraph in its entirety in the documentation or other materials
  10.  * provided with the distribution, and (3) all advertising materials mentioning
  11.  * features or use of this software display the following acknowledgement:
  12.  * ``This product includes software developed by the University of California,
  13.  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  14.  * the University nor the names of its contributors may be used to endorse
  15.  * or promote products derived from this software without specific prior
  16.  * written permission.
  17.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  18.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  19.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20.  */
  21. #ifndef lint
  22. static char rcsid[] =
  23.     "@(#) $Header: etherent.c,v 1.8 94/06/20 19:07:50 leres Exp $ (LBL)";
  24. #endif
  25.  
  26. #include <sys/types.h>
  27.  
  28. #include <ctype.h>
  29. #include <pcap.h>
  30. #include <pcap-namedb.h>
  31. #include <stdio.h>
  32.  
  33. #ifndef __GNUC__
  34. #define inline
  35. #endif
  36.  
  37. static inline int xdtoi(int);
  38. static inline int skip_space(FILE *);
  39. static inline int skip_line(FILE *);
  40.  
  41. /* Hex digit to integer. */
  42. static inline int
  43. xdtoi(c)
  44.     register int c;
  45. {
  46.     if (isdigit(c))
  47.         return c - '0';
  48.     else if (islower(c))
  49.         return c - 'a' + 10;
  50.     else
  51.         return c - 'A' + 10;
  52. }
  53.  
  54. static inline int
  55. skip_space(f)
  56.     FILE *f;
  57. {
  58.     int c;
  59.  
  60.     do {
  61.         c = getc(f);
  62.     } while (isspace(c) && c != '\n');
  63.  
  64.     return c;
  65. }
  66.  
  67. static inline int
  68. skip_line(f)
  69.     FILE *f;
  70. {
  71.     int c;
  72.  
  73.     do
  74.         c = getc(f);
  75.     while (c != '\n' && c != EOF);
  76.  
  77.     return c;
  78. }
  79.  
  80. struct pcap_etherent *
  81. pcap_next_etherent(FILE *fp)
  82. {
  83.     register int c, d, i;
  84.     char *bp;
  85.     static struct pcap_etherent e;
  86.     static int nline = 1;
  87.  top:
  88.     while (nline) {
  89.         /* Find addr */
  90.         c = skip_space(fp);
  91.         if (c == '\n')
  92.             continue;
  93.         /* If this is a comment, or first thing on line
  94.            cannot be ethernet address, skip the line. */
  95.         else if (!isxdigit(c))
  96.             c = skip_line(fp);
  97.         else {
  98.             /* must be the start of an address */
  99.             for (i = 0; i < 6; i += 1) {
  100.                 d = xdtoi(c);
  101.                 c = getc(fp);
  102.                 if (c != ':') {
  103.                     d <<= 4;
  104.                     d |= xdtoi(c);
  105.                     c = getc(fp);
  106.                 }
  107.                 e.addr[i] = d;
  108.                 if (c != ':')
  109.                     break;
  110.                 c = getc(fp);
  111.             }
  112.             nline = 0;
  113.         }
  114.         if (c == EOF)
  115.             return 0;
  116.     }
  117.  
  118.     /* If we started a new line, 'c' holds the char past the ether addr,
  119.        which we assume is white space.  If we are continuing a line,
  120.        'c' is garbage.  In either case, we can throw it away. */
  121.  
  122.     c = skip_space(fp);
  123.     if (c == '\n') {
  124.         nline = 1;
  125.         goto top;
  126.     }
  127.     else if (c == '#') {
  128.         (void)skip_line(fp);
  129.         nline = 1;
  130.         goto top;
  131.     }
  132.     else if (c == EOF)
  133.         return 0;
  134.  
  135.     /* Must be a name. */
  136.     bp = e.name;
  137.     /* Use 'd' to prevent buffer overflow. */
  138.     d = sizeof(e.name) - 1;
  139.     do {
  140.         *bp++ = c;
  141.         c = getc(fp);
  142.     } while (!isspace(c) && c != EOF && --d > 0);
  143.     *bp = '\0';
  144.     if (c == '\n')
  145.         nline = 1;
  146.  
  147.     return &e;
  148. }
  149.